package com.humanres.activity;

import android.app.DatePickerDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.type.EmployeeEnum;
import com.ciko.code.utils.LogUtils;
import com.ciko.code.utils.Util;
import com.mywork.R;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2016/2/16.
 */
public class ResReviewActivity extends BaseActicity {

  private RelativeLayout rectchooseType;
  private RelativeLayout rectchooseTimeStart;
  private RelativeLayout rectchooseTimeEnd;

  private TextView tv_startTimeLeave;
  private TextView tv_endTimeLeave;
  private TextView tv_dayCountLeave;
  private TextView tv_Job;
  private  TextView tv_Name;
  private ImageView ivHanderIcon;

  private EditText etAdressOut;
  private EditText etresonLeave;

  private Button btnsubmitLeave;

  private DatePickerDialog startTimeDialog;

  private long startTime = 0;
  private long endTime = 0;

  private DatePickerDialog endTimeDialog;

  private int dayCount = 0;

  private String adress;
  private String reson = "";
  private User nextuser=null;

  @Override
  protected void initTopBar() {
    setTitle("报销申请");
    enableNavLeftImg();
  }

  @Override
  protected void initLayout() {
    setContentView(R.layout.activity_res_review);
  }

  @Override
  protected void initView() {

    btnsubmitLeave = (Button) findViewById(R.id.btn_submitLeave);
    tv_startTimeLeave = (TextView) findViewById(R.id.tv_startTimeLeave);
    tv_endTimeLeave = (TextView) findViewById(R.id.tv_endTimeLeave);
    tv_dayCountLeave = (TextView) findViewById(R.id.tv_dayCountLeave);
    etresonLeave = (EditText) findViewById(R.id.et_resonLeave);
    etAdressOut = (EditText) findViewById(R.id.etAdressOut);
    rectchooseTimeEnd = (RelativeLayout) findViewById(R.id.rect_chooseTimeEnd);
    rectchooseTimeStart = (RelativeLayout) findViewById(R.id.rect_chooseTimeStart);
    rectchooseType = (RelativeLayout) findViewById(R.id.rect_chooseType);
    ivHanderIcon = (ImageView) findViewById(R.id.ivHanderIcon);
    tv_Job = (TextView) findViewById(R.id.tvJob);
    tv_Name = (TextView) findViewById(R.id.tvName);
  }

  @Override
  protected void initAdapter() {

  }

  @Override
  protected void initDates() {
    //Calendar calendar = Calendar.getInstance(Locale.CHINA);
    //
    //startTimeDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    //  @Override
    //  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //    tv_startTimeLeave.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
    //    startTime = view.getCalendarView().getDate();
    //    endTimeDialog.getDatePicker().setMinDate(startTime);
    //
    //    if (endTime != 0 && startTime != 0) {
    //
    //      dayCount = (int) ((endTime - startTime) / (24 * 60 * 60 * 1000)) + 1;
    //
    //      tv_dayCountLeave.setText(String.valueOf(dayCount));
    //    }
    //
    //  }
    //}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    //
    //endTimeDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    //  @Override
    //  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //    tv_endTimeLeave.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
    //    endTime = view.getCalendarView().getDate();
    //
    //    if (endTime != 0 && startTime != 0) {
    //      dayCount = (int) ((endTime - startTime) / (24 * 60 * 60 * 1000)) + 1;
    //      tv_dayCountLeave.setText(String.valueOf(dayCount));
    //    }
    //  }
    //}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    //
    ////获取审批人
    //doRequestHttp(new GetNextPersonReq());
  }

  @Override
  protected void initListener() {
    //rectchooseTimeStart.setOnClickListener(this);
    //rectchooseTimeEnd.setOnClickListener(this);

    btnsubmitLeave.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    super.onClick(v);
    if(v==btnsubmitLeave){
      //if (check()) {
      //  doRequestHttp(new AddTripFlowReq(adress, startTime, endTime, dayCount, reson, "", nextuser.getId(), nextuser.getStaffName()));
      //}
    } else if(v==rectchooseTimeEnd){
      endTimeDialog.show();
    } else if(v==rectchooseTimeStart){
      startTimeDialog.show();
    } else if(v==rectchooseType){

    }
  }

  private boolean check() {

    adress = etAdressOut.getText().toString().trim();
    reson = etresonLeave.getText().toString().trim();

    if (TextUtils.isEmpty(adress)) {
      showToast("请输入出差地址");
      return false;
    } else if (startTime == 0) {
      showToast("请选择开始时间");
      return false;
    } else if (endTime == 0) {
      showToast("请选择结束时间");
      return false;
    } else if (dayCount < 0) {
      showToast("结束时间必须大于开始时间");
      return false;
    } else if (TextUtils.isEmpty(reson)) {
      showToast("请填写出差事由");
      return false;
    } else if(null==nextuser){
      showToast("请选择审批人");
      return false;
    }

    return true;
  }

  @Override
  public void onResponseSuccess(BaseResponse<?> response, String tag) {
    super.onResponseSuccess(response, tag);

    switch (tag) {
      case "AddTripFlowReq":
        showToast("提交成功");
        finish();
        break;
      case "GetNextPersonReq":
        nextuser = (User) response.getReturnObject();
        LogUtils.d("GetNextPersonReq", nextuser.getStaffName());
        if(null != nextuser){
          tv_Job.setText(EmployeeEnum.typeOfValue(nextuser.getUserPositionMark()));
          tv_Name.setText(nextuser.getStaffName());
          if(!TextUtils.isEmpty(nextuser.getUserHead())) Picasso.with(this).load(nextuser.getUserHead()).into(ivHanderIcon);
          if (nextuser.getUserPositionMark() == 9999) {
            Util.showToast(this, R.string.params_less_tip);
          }
        }
        break;
    }
  }

}
